package com.itheima.pinda.web.transportline;

import com.itheima.pinda.DTO.transportline.TransportTripsDto;
import com.itheima.pinda.DTO.transportline.TransportTripsTruckDriverDto;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.common.utils.Result;
import com.itheima.pinda.entity.transportline.PdTransportTrips;
import com.itheima.pinda.mapstruct.TransportDTO2Entity;
import com.itheima.pinda.service.PdTransportTripsService;
import com.itheima.pinda.service.PdTransportTripsTruckDriverService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 车次管理模块
 *
 * @author llhuang10
 * @date 2021/10/6 21:22
 */
@Slf4j
@Validated
@RestController
@Api(tags = "车次管理模块")
@RequestMapping("/base/transportLine/trips")
public class TransportTripsController {

    @Resource
    private TransportDTO2Entity dto2Entity;

    @Resource
    private PdTransportTripsTruckDriverService tripsTruckDriverService;

    @Resource
    private PdTransportTripsService transportTripsService;


    /**
     * 添加车次
     *
     * @param dto 车次信息
     * @return 车次信息
     */
    @PostMapping
    public TransportTripsDto saveTransportTrips(@Valid @RequestBody TransportTripsDto dto) {
        return transportTripsService.saveTransportTrips(dto);
    }

    /**
     * 根据id获取车次详情
     *
     * @param id 车次id
     * @return 车次信息
     */
    @GetMapping("/{id}")
    public TransportTripsDto findById(@NotBlank(message = "车次id不能为空!") @PathVariable("id") String id) {
        PdTransportTrips pdTransportTrips = transportTripsService.getById(id);
        if (pdTransportTrips != null) {
            return dto2Entity.conver(pdTransportTrips);
        } else {
            TransportTripsDto transportTripsDto = new TransportTripsDto();
            transportTripsDto.setId(id);
            return transportTripsDto;
        }
    }

    /**
     * 获取车次列表
     *
     * @param transportLineId 线路类型id
     * @param ids             车次id列表
     * @return 车次列表
     */
    @GetMapping
    public List<TransportTripsDto> findAll(@RequestParam(name = "transportLineId", required = false) String transportLineId,
                                           @RequestParam(name = "ids", required = false) List<String> ids) {
        return transportTripsService.findAll(transportLineId, ids);
    }

    /**
     * 更新车次信息
     *
     * @param id  车次id
     * @param dto 车次信息
     * @return 车次信息
     */
    @PutMapping("/{id}")
    public TransportTripsDto update(@NotBlank(message = "车次id不能为空!") @PathVariable("id") String id,
                                    @Valid @RequestBody TransportTripsDto dto) {
        dto.setId(id);
        PdTransportTrips pdTransportTrips = dto2Entity.conver(dto);
        transportTripsService.updateById(pdTransportTrips);
        return dto;
    }

    /**
     * 删除车次信息
     *
     * @param id 车次id
     * @return 返回信息
     */
    @PutMapping("/{id}/disable")
    public Result disable(@NotBlank(message = "车次id不能为空!") @PathVariable("id") String id) {
        PdTransportTrips pdTransportTrips = new PdTransportTrips();
        pdTransportTrips.setId(Long.parseLong(id));
        pdTransportTrips.setStatus(Constant.DATA_DISABLE_STATUS.byteValue());
        boolean res = transportTripsService.updateById(pdTransportTrips);
        return res ? Result.ok() : Result.error("删除车次信息失败, 请联系管理员!");
    }

    /**
     * 批量保存车次与车辆和司机关联关系
     *
     * @param transportTripsId 车次id
     * @param dtoList          车次与车辆和司机关联关系
     * @return 返回信息
     */
    @PostMapping("/{id}/truckDriver")
    public Result batchSaveTruckDriver(@NotBlank(message = "车次id不能为空!") @PathVariable("id") String transportTripsId,
                                       @RequestBody List<TransportTripsTruckDriverDto> dtoList) {
        return tripsTruckDriverService.batchSaveTruckDriver(transportTripsId, dtoList);
    }

    /**
     * 获取车次与车辆和司机关联关系列表
     *
     * @param transportTripsId 车次id
     * @param truckId          车辆id
     * @param userId           司机id
     * @return 车次与车辆和司机关联关系列表
     */
    @GetMapping("/truckDriver")
    public List<TransportTripsTruckDriverDto> findAllTruckDriverTransportTrips(
            @RequestParam(name = "transportTripsId", required = false) String transportTripsId,
            @RequestParam(name = "truckId", required = false) String truckId,
            @RequestParam(name = "userId", required = false) String userId) {
        return tripsTruckDriverService.findAllTruckDriverTransportTrips(transportTripsId, truckId, userId);
    }

}
